/*
 * Copyright (c) 2015 DHC Corporation.
 */

package cn.com.dhc.danlu.widget.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.ActivityUtils;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.widget.CustomAbstractListView;
import cn.com.dhc.danlu.widget.NoScrollListView;
import cn.com.dhc.danlu.widget.holder.DeliverySellerHolder;
import cn.com.hd.domain.entity.entityext.GoodsExt;
import cn.com.hd.domain.entity.entityext.OrderExt;

/**
 * 配送中心（销售）ListView用适配器
 *
 * @author MuWei
 * @version 1.00 2015/02/09 新建
 */
public class DeliverySellerListAdapter extends CustomAbstractListAdapter<OrderExt> {
    /**
     * 默认商品数量
     */
    private static final String DEFAULT_COUNT = String.valueOf(0);
    private Context context;

    /**
     * 构造函数
     *
     * @param listView
     *         自定义下拉刷新ListView
     * @param model
     *         数据对象
     */
    public DeliverySellerListAdapter(CustomAbstractListView listView, List<OrderExt> model, Context context) {
        super(listView, model);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 画面控件
        DeliverySellerHolder holder;

        // 提升ListView运行效率，在布局为空时才初始化
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.view_delivery_seller_item, null);
            holder = new DeliverySellerHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (DeliverySellerHolder) convertView.getTag();
        }

        // 获取订单数据
        final OrderExt model = (OrderExt) getItem(position);
        // 设置数据
        setData(holder, model);
        return convertView;
    }

    /**
     * 设置数据
     *
     * @param holder
     *         控件集合
     * @param model
     *         订单数据
     */
    private void setData(final DeliverySellerHolder holder, final OrderExt model) {
        // 设置订单编号
        holder.txtOrderNo.setText(model.getOrderNo());

        if (TextUtils.isEmpty(model.getStatus())) {
            holder.txtOrderStatus.setVisibility(View.GONE);
        } else {
            // 设置订单状态
            //            holder.txtOrderStatus.setText(CommonUtils.getOrderStatus(getListView().getDelegate().getContext(), model.getStatus()));
            holder.txtOrderStatus.setText(CommonUtils.getOrderStatus(getListView().getContext(), model.getStatus()));
            holder.txtOrderStatus.setVisibility(View.VISIBLE);
        }

        // 没有商品
        if (model.getGoodsList() == null || model.getGoodsList().isEmpty()) {
            // 商品总数
            holder.txtGoodsTotal.setText(DEFAULT_COUNT);
        } else {
            // 商品总数
            int count = 0;
            for (GoodsExt goodsExt : model.getGoodsList()) {
                // 计算商品总数
                count += goodsExt.getNum();
            }

            // 设置商品总数
            holder.txtGoodsTotal.setText(String.valueOf(count));
        }

        holder.txtTotalMoney.setText(model.getAfter_tax_total());

        if (model.getGoodsList() != null) {
            // 商品列表Adapter
            //            DeliverySellerGoodsListAdapter adapter = new DeliverySellerGoodsListAdapter(getListView().getDelegate().getContext(), model.getGoodsList());
            DeliverySellerGoodsListAdapter adapter = new DeliverySellerGoodsListAdapter(getListView().getContext(), model.getGoodsList());
            holder.lstDeliveryGoods.setAdapter(adapter);
            // 商品列表点击监听
            holder.lstDeliveryGoods.setOnItemClickListener(new NoScrollListView.OnItemClickListener() {
                @Override
                public void onItemClick(NoScrollListView parent, int position) {
                }
            });
        }

        // 设置监听
        setListener(holder, model);
    }

    /**
     * 设置监听
     *
     * @param holder
     *         控件集合
     * @param model
     *         订单数据
     */
    private void setListener(DeliverySellerHolder holder, final OrderExt model) {
        // 订单选中复选框监听
        holder.chkSelected.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO 相关操作，应该是记录该订单ID或是就是记录此订单对象
            }
        });
        // 查看详情按钮添加点击监听
        holder.btnOrderDetail.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // 跳转至订单详细画面
                //                ActivityUtils.startDeliveryDetailActivity(getListView().getDelegate().getContext(), model.getOrderId());
                ActivityUtils.startDeliveryDetailActivity(getListView().getContext(), model.getOrderId());

            }
        });

        // 配送完成按钮添加点击监听
        holder.btnOrderDeliveryComplete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        // 接单按钮添加点击监听，TODO跳转到支付页面
        holder.btnOrderReceive.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            }
        });
    }
}
